package org.rency.ues.annotation.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.rency.ues.annotation.Encrypt;
import org.rency.ues.annotation.service.EncryptService;
import org.rency.ues.annotation.support.MultiAnnotionData;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * 加密切入点
 * @author: user_rcy@163.com
 * </br>Created on 2016年7月22日
 * @version: V1.0.0
 * @since JDK 1.8
 */
@Component
@Aspect
public class EncryptAdvice extends UesAdviceBase {
	
	@Resource(name="encryptService")
	private EncryptService encryptService;

    @Pointcut("@annotation(org.rency.ues.annotation.Encrypt)")
    public void encrypt() {
    }

    @Around("encrypt()")
    public Object aroundEncrypt(ProceedingJoinPoint pjp) throws Throwable {
        Method currentMethod = this.getCurrentMethod(pjp);
        Annotation annotation = this.getMethodAnnotation(currentMethod, Encrypt.class);
        MultiAnnotionData multiData = this.buildEncryptAnnotationData(pjp,currentMethod,annotation);
        Object[] args = pjp.getArgs();
        Object[] newargs = encryptService.process(args, multiData);
        Object ret = pjp.proceed(newargs);
        return ret;
    }

}
